<template>
  <div>
    <tag-list class="mg-btm" :list=prodTypeList :active=data.product_kind @onClick=changeProdType />

    <el-form :inline="true" :model="searchData">
      <el-form-item label="">
        <el-select v-model="data.order_state" placeholder="订单状态" clearable>
          <el-option
            v-for="item in statusList"
            :key="item.key"
            :label="item.name"
            :value="item.key+''"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间：">
        <date-range-picker v-model="order_date" />
      </el-form-item>
      <el-form-item label="游玩日期：">
        <date-range-picker v-model="avail_date" />
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="data.query_order" clearable placeholder="单号/预订人姓名/手机"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="data.query_product" clearable placeholder="产品ID/名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doSearch">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {
    Row,
    Col,
    Form,
    FormItem,
    Select,
    Option,
    Input,
    Button
  } from 'element-ui'
  import DateRangePicker from "@cmpt_pc/date-range-picker"
  import {
    CellGroup,
    Field,
  } from 'vant'
  import TagList from "@cmpt_pc/tag-list"

  import {
    getConfig
  } from '@api/modules'
  import {
    ORDER_STATUS_QUERY_LIST,
  } from '@data/dic/order'
  import {
    replaceEmpty
  } from '@utils/utils'

  const PROD_KIND = [{
    name: '全部',
    key: '-999'
  }]

  export default {
    name: 'order-search',
    components: {
      [Row.name]: Row,
      [Col.name]: Col,
      [CellGroup.name]: CellGroup,
      [Field.name]: Field,
      [Button.name]: Button,

      [Form.name]: Form,
      [FormItem.name]: FormItem,
      [Select.name]: Select,
      [Option.name]: Option,
      [Input.name]: Input,

      TagList,
      DateRangePicker,
    },
    props: {},

    data() {
      return {
        prodTypeList: PROD_KIND,
        statusList: ORDER_STATUS_QUERY_LIST,
        data: {
          product_kind: this.$getUrlParam('product_kind') || PROD_KIND[0].key,
          order_state: this.$getUrlParam('order_state') || '',
          avail_begin: this.$getUrlParam('avail_begin') || '',
          avail_end: this.$getUrlParam('avail_end') || '',
          query_order: this.$getUrlParam('query_order') || '',
          query_product: this.$getUrlParam('query_product') || '',
        },
        order_date: [this.$getUrlParam('order_begin') || '', this.$getUrlParam('order_end') || ''],
        avail_date: [this.$getUrlParam('avail_begin') || '', this.$getUrlParam('avail_end') || ''],
        searchData: {},
      }
    },

    watch: {
      order_date: {
        handler(newData) {
          this.data.order_begin = newData[0]
          this.data.order_end = newData[1]
        },
        deep: true,
        immediate: true,
      },
      avail_date: {
        handler(newData) {
          this.data.avail_begin = newData[0]
          this.data.avail_end = newData[1]
        },
        deep: true,
        immediate: true,
      }
    },

    methods: {
      async getProdType() {
        getConfig('product_kind')
          .then(res => {
            this.prodTypeList = PROD_KIND.concat([])
            if (!res.data.product_kind)
              return
            res.data.product_kind.forEach(item => {
              this.prodTypeList.push({
                name: item.name,
                key: item.kind
              })
            })
          })
          .catch(err => {
            this.prodTypeList = PROD_KIND.concat([])
          })
      },

      doSearch() {
        this.$emit('doSearch', {
          ...this.data,
          product_kind: replaceEmpty(this.data.product_kind, '-999'),
          order_state: replaceEmpty(this.data.order_state, '-999')
        })
      },

      changeProdType(key) {
        this.data.product_kind = key
        this.doSearch({
          product_kind: this.data.product_kind
        })
      }
    },

    created() {
      this.getProdType()
      this.doSearch()
    },
  }
</script>
